package com.yeshine.opendata.controller.front.my.org.dataset.data;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.Data;
import com.yeshine.opendata.entity.DataSet;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.entity.Org;
import com.yeshine.opendata.service.IDataService;
import com.yeshine.opendata.service.IDataSetService;
import com.yeshine.opendata.service.IDictionaryService;
import com.yeshine.opendata.service.IOrgService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import com.yeshine.opendata.utils.mybatis.DashboardMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by taohj on 2014/7/3.
 */
@Controller
@RequestMapping("/front/my/org/dataset/data/")
public class MyOrgDataController extends AbstractFrontControllerBase {

    @Autowired
    IDataService service;


    @Autowired
    private IDataService dataService;


    @Autowired
    private IOrgService orgService;


    @Autowired
    private IDictionaryService dictionaryService;

    @Autowired
    private IDataSetService dataSetService;


    @RequestMapping("createV")
    public String createV(@RequestParam(value = "dataset", required = false) Integer dataset) {
        if (dataset != null) {
            setRequestAttr("dataset", dataset);
        }
        setRequestAttr("orgList", orgService.getList(null).getList());
        setRequestAttr("visibilitys", dictionaryService.getList(new SysDictionary("VISIBILITY")).getList());
        List<DashboardMenu> dashboardMenus = new ArrayList<DashboardMenu>();
        dashboardMenus.add(new DashboardMenu("新增数据", true));
        setRequestAttr("dashboardMenus", dashboardMenus);


        DataSet dataSet=new DataSet();
        dataSet.setCreater(getCurrentUser());
        PageResult<DataSet> dataSetPageResult=new PageResult<DataSet>(1,999999999);
        dataSetPageResult.setT(dataSet);
        dataSetPageResult.setWhere(" and ds.org is null ");
        dataSetService.getByPage(dataSetPageResult);
        setRequestAttr("datasets",dataSetPageResult.getRows());
        return "/front/my/org/dataset/data/create";
    }


    @RequestMapping("create")
    public String create(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "license", required = false) String license,
            @RequestParam(value = "org", required = false) Integer org,
            @RequestParam(value = "storage", required = false) Integer storage,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "visibility", required = false) Integer visibility,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "GUID", required = false) String GUID,
            @RequestParam(value = "dataset", required = false) Integer dataset,
            @RequestParam(value = "file", required = false) MultipartFile file

    ) {
        Data data = new Data();
        data.setDataset(new DataSet(dataset));
        data.setCreater(getCurrentUser());
        data.setTitle(title);
        data.setDescription(description);
        data.setTags(tags);
        data.setLicense(license);
        data.setVersion(version);
        if (org != null) {
            data.setOrg(new Org(org));
        }
        data.setStorage(new SysDictionary(storage));
        data.setUrl(url);
        data.setVisibility(new SysDictionary(visibility));
        data.setStatus(new SysDictionary(181));
        data.setVersion(version);
        data.setGUID(GUID);


        data.setStatus(new SysDictionary(111));
        data.setAuditStatus(new SysDictionary(181));
        data.setFile(file);
        Result<Data> result = service.create(data, getRequest());
        if (result.isSuccess()) {
            return "redirect:/front/my/org/dataset/data/list?dataset=" + dataset;
        } else {
            return setMessage("数据新增失败", "新增失败");
        }


    }


    @RequestMapping("remove")
    public String remove(
            @RequestParam("id") Integer id
    ) {
        Data data = service.getById(id).getTarget();
        if (data == null) {
            return setMessage("删除失败", "该数据不存在");
        }
        Result<Data> result = service.remove(data);
        return getReturnString(result, "redirect:/front/my/org/dataset/data/list?dataset=" + data.getDataset().getId(), setMessage("删除失败", result.getMessage()));
    }


    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam("id") Integer id
    ) {
        Data data = service.getById(id).getTarget();
        setRequestAttr("data", data);

        List<DashboardMenu> dashboardMenus = new ArrayList<DashboardMenu>();
        dashboardMenus.add(new DashboardMenu("更新数据", true));
        setRequestAttr("dashboardMenus", dashboardMenus);
        return "/front/my/org/dataset/data/modify";
    }


    @RequestMapping("modify")
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "license", required = false) String license,
            @RequestParam(value = "org", required = false) Integer org,
            @RequestParam(value = "storage", required = false) Integer storage,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "visibility", required = false) Integer visibility,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "GUID", required = false) String GUID,
            @RequestParam(value = "dataset", required = false) Integer dataset,
            @RequestParam(value = "file", required = false) MultipartFile file

    ) {
        Data data = new Data();
        data.setId(id);
        data.setCreater(getCurrentUser());
        data.setTitle(title);
        data.setDescription(description);
        data.setTags(tags);
        data.setLicense(license);
        if (org != null) {
            data.setOrg(new Org(org));
        }
        data.setStorage(new SysDictionary(storage));
        data.setUrl(url);
        data.setVisibility(new SysDictionary(visibility));

        data.setStatus(new SysDictionary(111));
        data.setAuditStatus(new SysDictionary(181));
        data.setVersion(version);
        data.setGUID(GUID);
        data.setFile(file);
        if (dataset != null) {
            data.setDataset(new DataSet(dataset));
        }
        Result<Data> result = service.modify(data, getRequest());

        data = service.getById(data.getId()).getTarget();
        return getReturnString(result, "redirect:/front/my/org/dataset/data/list?dataset=" + data.getDataset().getId(), setMessage("修改失败", result.getMessage()));
    }

    @RequestMapping("list")
    public String list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size", required = false) Integer size,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "dataset", required = false) Integer dataset,
            @RequestParam(value = "order", required = false) String order,
            @RequestParam(value = "visibility", required = false) Integer visibility

    ) {
        PageResult<Data> pageResult = new PageResult<Data>();
        pageResult.setSize(size);
        pageResult.setPage(page);

        Data data = new Data();
        if (StringUtils.hasLength(title)){
            data.setTitle(title);
        }
        if (StringUtils.hasLength(order)){
            pageResult.setSortBy(order);
            pageResult.setSortColumn(" data.title ");
        }

        if (visibility!=null){
            data.setVisibility(new SysDictionary(visibility));
        }

        data.setCreater(getCurrentUser());
        if (dataset != null) {
            data.setDataset(new DataSet(dataset));
        }
        pageResult.setT(data);
        Result<Data> result = service.getByPage(pageResult);
        setRequestAttr("result", result);

        DataSet dataSet=new DataSet();
        dataSet.setCreater(getCurrentUser());
        PageResult<DataSet> dataSetPageResult=new PageResult<DataSet>(1,999999999);
        dataSetPageResult.setT(dataSet);
        dataSetPageResult.setWhere(" and ds.org is null ");
        dataSetService.getByPage(dataSetPageResult);
        setRequestAttr("datasets",dataSetPageResult.getRows());
        return "/front/my/org/dataset/data/list";
    }


    @RequestMapping("view")
    public String detail(
            @RequestParam("id") Integer id
    ) {


        setRequestAttr("basePath", super.getBasePath());

        Data data = service.getById(id).getTarget();
        if (data == null) {
            return setMessage("访问失败", "该数据不存在");
        } else {
            setRequestAttr("data", data);
            return "/front/my/data/view";
        }
    }


    @RequestMapping("search")
    public String search(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "orgIds", required = false) Integer[] orgIds,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "formats", required = false) String[] formats,
            @RequestParam(value = "key", required = false) String key
    ) {
        page = page != null ? page : 1;
        size = size != null ? size : 10;
        PageResult<Data> pageResult = new PageResult<Data>();
        pageResult.setPage(page);
        pageResult.setSize(size);
        pageResult.setKey(key);

        Data data = new Data();

        if (StringUtils.hasLength(tags)) {
            data.setTags(tags);
        }

        if (orgIds != null && orgIds.length > 0) {
            data.setOrgIds(orgIds);
        }

        if (formats != null && formats.length > 0) {
            data.setFormats(formats);
        }


        pageResult.setT(data);

        Result<Data> result = service.search(pageResult);

        setRequestAttr("result", result);
        setRequestAttr("key", key);
        System.out.println(key);

        Result<Org> orgResult = orgService.getList(null);
        setRequestAttr("orgResult", orgResult);
        setRequestAttr("tags", dataService.getGroupByTags(null));
        setRequestAttr("format", dataService.getGroupByFormat(null));

        setRequestAttr("orgIds", orgIds);
        setRequestAttr("formats", formats);
        setRequestAttr("tag", tags);
        return "/front/my/data/search";
    }


    @RequestMapping("auditV")
    public String auditV(
            @RequestParam(value = "page", required = false,defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false,defaultValue = "20") Integer size,
            @RequestParam(value = "auditStatus",required = false) Integer auditStatus,
            @RequestParam("org") Integer org,
            @RequestParam(value = "dataset",required = false) Integer dataset
    ) {
        Data data = new Data();
        data.setDataset(new DataSet(dataset));
        data.getDataset().setOrg(orgService.getById(org).getTarget());
        data.setAuditStatus(new SysDictionary(auditStatus));
        PageResult<Data> pageResult = new PageResult<Data>(page, size);
        pageResult.setT(data);
        Result<Data> result = service.getByPage(pageResult);

        setRequestAttr("result",result);


        List<DashboardMenu> dashboardMenus = new ArrayList<DashboardMenu>();
        dashboardMenus.add(new DashboardMenu("数据审批", true));
        setRequestAttr("dashboardMenus", dashboardMenus);

        return "/front/my/org/dataset/data/audit";
    }


    @RequestMapping("audit")
    public String audit(
            @RequestParam("auditStatus") Integer auditStatus,
            @RequestParam("datas") Integer[] datas,
            @RequestParam("org") Integer org
    ) {
        Result<Data> result = service.audit(auditStatus, datas);
        return result.isSuccess() ? "redirect:/front/my/org/dataset/data/auditV?org="+org : setMessage("操作失败",result.getMessage());
    }

}
